﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
//using Client.Download;
namespace Yugi.Client.Converter
{
    partial class ConvertData
    {
        public List<byte> Result_Send = new List<byte>();
        List<object> Result = new List<object>();

        #region DownloadFile
        
        
        //private static DownloadFile dowload_file;
        //private static bool try_connect;
        //public static void Init_DownloadFile(object param)
        //{
        //    List<string> path = param as List<string>;
        //    try
        //    {
        //        dowload_file = new DownloadFile(path[0].ToString(),2010, path[1].ToString());
        //        try_connect = true;
        //        MyConsole.WriteLine("Connect Success to Card Server");
               
        //    }
        //    catch (Exception ex)
        //    {
                
        //        MyConsole.WriteLine("Connect Error Retry later......");
        //        System.Threading.Thread.Sleep(1000);
        //        dowload_file = null;
        //        Init_DownloadFile(path);
        //    }
        //}

        #endregion

        public void ConvertReceive(Common.YMSG message, byte[] data, SupervisorModel supervisor)
        {
           
                Result.Clear();
                string[] arr= message.ToString().Split('_');
                
                // Tao viet them khuc nay de test do mày kiểm tra sai TURN_ENDTURN
                List<string> tmp = arr.ToList();
                tmp.Add(" ");
                arr = tmp.ToArray();
                //////////////////////////////////////////////////////////////
                 
                if (arr[0] == "TURN" && arr[2] == "BEGIN")
                {
                    Convert_BeginSignal(data);
                }
                else if (arr[0] == "TURN" && arr[2] == "END")
                {
                    Convert_EndSignal(data);
                }
                else
                {
                    Type t = typeof(ConvertData);
                    MethodInfo handler = t.GetMethod("ConvertData_" + message.ToString(), BindingFlags.NonPublic | BindingFlags.Instance);
                    handler.Invoke(this, new object[] { data });
                }

                supervisor.ReciveMessageFromServer(message, Result.ToArray());
            
          
        }
        public void ConvertSend(Common.YMSG message, params object[] param)
        {
            Result_Send.Clear();
            Result_Send.Add((byte)message);
            Type t = typeof(ConvertData);
            MethodInfo handler = t.GetMethod("ConvertSendData_" + message.ToString(), BindingFlags.NonPublic | BindingFlags.Instance);
            handler.Invoke(this, new object[] { param });
        }
        private void Convert_BeginSignal(byte[]input)
        {
            int p_id = (int)input[0];
            Result.Add(p_id);
        }
        private void Convert_EndSignal(byte[] input)
        {

        }
    }
}
